<template>
  <div class="shape-pool">
    <div class="category" v-for="item in shapeList" :key="item.type">
      <div class="category-name">{{item.type}}</div>
      <div class="shape-list">
        <div class="shape-item" v-for="(shape, index) in item.children" :key="index">
          <div class="shape-content" draggable="true" @dragend='selectShape($event, shape)'>
            <svg 
              overflow="visible" 
              width="30"
              height="30"
            >
              <g 
                :transform="`scale(${30 / shape.viewBox}, ${30 / shape.viewBox}) translate(0,0) matrix(1,0,0,1,0,0)`"
              >
                <path 
                  class="shape-path"
                  vector-effect="non-scaling-stroke" 
                  stroke-linecap="butt" 
                  stroke-miterlimit="8"
                  stroke-linejoin
                  fill="transparent"
                  stroke="currentColor"
                  stroke-width="2" 
                  :d="shape.path"
                ></path>
              </g>
            </svg>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { SHAPE_LIST, ShapePoolItem } from '@/configs/shapes'

export default defineComponent({
  name: 'shape-pool',
  emits: ['select', 'close'],
  setup(props, { emit }) {
    const shapeList = SHAPE_LIST

    const selectShape = ($event: MouseEvent, shape: ShapePoolItem) => {
      emit('select', {shape, $event})
    }
    const close = () => {
      emit('close')
    }
    return {
      shapeList,
      selectShape,
      close
    }
  },
})
</script>

<style lang="scss" scoped>
.shape-pool {
  width: 290px;
  height: calc(100% - 80px);
  overflow-y: scroll;
  margin-bottom: -12px;
  margin-right: -12px;
  padding: 0 0 12px 12px;
}
.category-name {
  width: 100%;
  font-size: 14px;
  margin-bottom: 10px;
  color: $contentTextColor;
  padding: 2px 0 2px 0;
}
.shape-list {
  @include flex-grid-layout();

  margin-bottom: 10px;
}
.shape-item {
  margin: 10px 7px;
  width: 30px;
  height: 0;
  padding-bottom: 10%;
  flex-shrink: 0;
  position: relative;
  cursor: move;
}
.shape-content {
  @include absolute-0();

  display: flex;
  justify-content: center;
  align-items: center;
  color: $contentTextColor;
  &:hover .shape-path {
    stroke: $themeColor;
  }

  svg:not(:root) {
    overflow: visible;
  }
}
</style>